package cn.edu.swu.io_2;

import java.io.IOException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ImageManager imageManager = new ImageManager();

        while (true){
            // 打印菜单
            System.out.println("=== 图片下载系统 ===");
            System.out.println("输入数字选项（1~4）进行操作");
            System.out.println("1. 下载图片");
            System.out.println("2. 查看所有图片");
            System.out.println("3. 搜索图片");
            System.out.println("4. 退出");
            System.out.println("您的选择是？");


            int choice = scanner.nextInt();

            // 根据不同选择运行不同功能
            switch (choice){
                case 1:
                    System.out.println("请输入图片链接:");
                    String url = scanner.next();

                    if (url.isEmpty()){
                        System.out.println("链接不能为空!");
                        break;
                    }

                    try {
                        imageManager.downloadImage(url);
                        System.out.println("下载任务已完成！");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;


                case 2:
                    System.out.println("以下是全部图片相关信息：");
                    imageManager.listImages();
                    break;

                case 3:
                    System.out.println("请输入搜索图片名：");
                    String imageName = scanner.next();

                    if (imageName.isEmpty()){
                        System.out.println("搜索词不能为空!");
                        break;
                    }

                    imageManager.searchImage(imageName);
                    break;

                case 4:
                    System.out.println("感谢使用，再见！");
                    scanner.close();
                    return;

                default:
                    System.out.println("无效选择！请输入1~4之间的数字");
            }

            System.out.print("按回车键继续...");
            scanner.nextLine(); // 清掉无效信息
            scanner.nextLine(); // 等待用户回车
        }


    }

}
